package com.zboot.zbootadmin.common.rest;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

// should handle all exception for classes annotated with
@ControllerAdvice(annotations = RestController.class)
public class RestExceptionHandler {

  @ExceptionHandler(Exception.class)
  public ResponseEntity<ErrorEntity> resourceNotFoundException(HttpServletRequest request, Exception ex) {
    ErrorEntity entity = new ErrorEntity();
    entity.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    entity.setError(HttpStatus.INTERNAL_SERVER_ERROR.name());
    entity.setTimestamp(new Date().getTime());
    entity.setExceptionClass(ex.getClass().getName());
    entity.setMessage(ex.getMessage());
    entity.setPath(request.getRequestURL().toString());
    return new ResponseEntity<>(entity, HttpStatus.INTERNAL_SERVER_ERROR);
  }
}
